package groupb6.shopsystem.controller;


import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import groupb6.shopsystem.pojo.entity.Order;
import groupb6.shopsystem.pojo.entity.Store;
import groupb6.shopsystem.pojo.entity.User;
import groupb6.shopsystem.service.AdminService;
import groupb6.shopsystem.util.JWTUtil;
import groupb6.shopsystem.util.enums.IdentityEnum;
import groupb6.shopsystem.util.response.UniversalResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "3:管理员主页")
@ApiSupport(author = "付盛原")
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @ApiOperation("根据店铺id查询店铺详情")
    @GetMapping("/store/info/getByStoreId")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 2004, message = "用户权限不足"),
            @ApiResponse(code = 7000, message = "店铺不存在"),
    })
    @ResponseBody
    public UniversalResponse<Store> getStoreInfoByStoreId(
            @RequestParam Integer storeId) {
        User user = JWTUtil.getCurrentUser();
        if (user.getIdentity().equals(IdentityEnum.MANAGER.getIdentity())) {
            return adminService.getStoreDetailByStoreId(storeId);
        } else {
            return new UniversalResponse<>(2004, "用户权限不足");
        }
    }

    @ApiOperation("根据状态获取店铺")
    @GetMapping("/store/info/getByStatus")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 2004, message = "用户权限不足"),
    })
    @ResponseBody
    public UniversalResponse<List<Store>> getStoresByStatus(
            @RequestParam(defaultValue = "0") Integer storeStatus) {
        User user = JWTUtil.getCurrentUser();
        if (user.getIdentity().equals(IdentityEnum.MANAGER.getIdentity())) {
            return adminService.getStoresByStatus(storeStatus);
        } else {
            return new UniversalResponse<>(2004, "用户权限不足");
        }
    }

    @ApiOperation("获取全部店铺信息")
    @GetMapping("/store/info/getAll")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 2004, message = "用户权限不足"),
    })
    @ResponseBody
    public UniversalResponse<List<Store>> getAllStore() {
        User user = JWTUtil.getCurrentUser();
        if (user.getIdentity().equals(IdentityEnum.MANAGER.getIdentity())) {
            return adminService.getAllStore();
        } else {
            return new UniversalResponse<>(2004, "用户权限不足");
        }
    }

    @ApiOperation("审批店铺")
    @GetMapping("/store/approve")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 2004, message = "用户权限不足"),
            @ApiResponse(code = 7000, message = "店铺不存在"),
            @ApiResponse(code = 7001, message = "店铺无需批准"),
    })
    @ResponseBody
    public UniversalResponse<?> approveStore(
            @RequestParam Integer storeId,
            @RequestParam Boolean approve) {
        User user = JWTUtil.getCurrentUser();
        if (user.getIdentity().equals(IdentityEnum.MANAGER.getIdentity())) {
            System.out.println(1);
            return adminService.approveStore(storeId, approve, user.getId());

        } else {
            return new UniversalResponse<>(2004, "用户权限不足");
        }
    }

    @ApiOperation("列出发货订单")
    @GetMapping("/listShippedOrders")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 2004, message = "用户权限不足")
    })
    @ResponseBody
    public UniversalResponse<List<Order>> listShippedOrders() {
        User currentUser = JWTUtil.getCurrentUser();
        if (currentUser.getIdentity().equals(IdentityEnum.MANAGER.getIdentity())) {
            return adminService.listShippedOrders();
        } else {
            return new UniversalResponse<>(2004, "用户权限不足");
        }
    }

    @ApiOperation("收货所有发货订单")
    @GetMapping("/deliveredShippedOrders")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 2004, message = "用户权限不足")
    })
    @ResponseBody
    public UniversalResponse<?> deliveredShippedOrders() {
        User currentUser = JWTUtil.getCurrentUser();
        if (currentUser.getIdentity().equals(IdentityEnum.MANAGER.getIdentity())) {
            return adminService.deliveredShippedOrders();
        } else {
            return new UniversalResponse<>(2004, "用户权限不足");
        }
    }

    @ApiOperation("订单收货")
    @GetMapping("/OrderDelivered")
    @ApiResponses(value =
            {
                    @ApiResponse(code = 1000, message = "成功"),
                    @ApiResponse(code = 2004, message = "用户权限不足"),
                    @ApiResponse(code = 3001, message = "订单状态错误"),
                    @ApiResponse(code = 3005, message = "订单不存在")
            })
    @ResponseBody
    public UniversalResponse<?> orderDelivered(
            @RequestParam Integer orderId
    ) {
        User currentUser = JWTUtil.getCurrentUser();
        if (currentUser.getIdentity().equals(IdentityEnum.MANAGER.getIdentity())) {
            return adminService.orderDelivered(orderId);
        } else {
            return new UniversalResponse<>(2004, "用户权限不足");
        }
    }
}
